package com.cloud.common.result;

import java.io.Serializable;

import com.cloud.common.exception.CloudException;

public class ExecuteResult<T> implements Serializable {

	private static final long serialVersionUID = -4467073382353735654L;

	public static final String SUCCESS = "SUCCESS";
	public static final String FAIL = "FAIL";
	public static final String sucMsg = "操作成功";
	public static final String failMsg = "操作失败";

	/**
	 * 执行成功
	 */
	protected String success = FAIL;
	/**
	 * 错误信息
	 */
	protected String msg;
	/**
	 * 错误码
	 */
	protected int code;

	protected T data;

	/**
	 * 请求序号
	 *
	 * @return
	 */
	protected String bizSeqNo;

	/**
	 * 事务时间
	 */
	protected Long transactionTime;

	public ExecuteResult() {
	}

	public ExecuteResult(String success, String msg, int code, T data) {
		this.success = success;
		this.msg = msg;
		this.code = code;
		this.data = data;
	}

	public ExecuteResult(String success, String msg, int code) {
		this.success = success;
		this.msg = msg;
		this.code = code;
	}

	public ExecuteResult(String success, String msg) {
		this.success = success;
		this.msg = msg;
	}

	public ExecuteResult(String success, String msg, T data) {
		this.success = success;
		this.msg = msg;
		this.data = data;
	}

	public ExecuteResult(String success, int code) {
		this.success = success;
		this.code = code;
	}

	public ExecuteResult(String success, int code, T data) {
		this.success = success;
		this.code = code;
		this.data = data;
	}

	public static <T> ExecuteResult<T> ok(String success, String msg, int code, T data) {
		return new ExecuteResult<>(SUCCESS, sucMsg, 0, data);
	}

	public static <T> ExecuteResult<T> ok() {
		return ok(SUCCESS, null, 0, null);
	}

	public static <T> ExecuteResult<T> ok(String msg) {
		return ok(SUCCESS, msg, 0, null);
	}

	public static <T> ExecuteResult<T> ok(T value) {
		return ok(SUCCESS, null, 0, value);
	}

	public static <T> ExecuteResult<T> ok(String msg, T value) {
		return ok(SUCCESS, msg, 0, value);
	}

	public static <T> ExecuteResult<T> fail(String success, String msg, int code) {
		return new ExecuteResult<>(FAIL,
				failMsg,  -1);
	}

	public static <T> ExecuteResult<T> fail() {
		return fail(FAIL, failMsg, -1);
	}

	public static <T> ExecuteResult<T> fail(String message) {
		return fail(FAIL, message, -1);
	}

	public static <T> ExecuteResult<T> fail(int errorCode) {
		return fail(FAIL, failMsg, errorCode);
	}

	public static <T> ExecuteResult<T> fail(int errorCode, String message) {
		return fail(FAIL, message, errorCode);
	}

	public static <T> ExecuteResult<T> fail(CloudException jhfException) {
		ExecuteResult<T> result = new ExecuteResult<>();
		result.code = jhfException.getCode();
		result.msg = jhfException.getMessage();
		return result;
	}

	public String getSuccess() {
		return success;
	}

	public void setSuccess(String success) {
		this.success = success;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getBizSeqNo() {
		return bizSeqNo;
	}

	public void setBizSeqNo(String bizSeqNo) {
		this.bizSeqNo = bizSeqNo;
	}

	public Long getTransactionTime() {
		return transactionTime;
	}

	public void setTransactionTime(Long transactionTime) {
		this.transactionTime = transactionTime;
	}

//	public static <T> ExecuteResult<T> fail(ReturnCode returnCode) {
//		ExecuteResult<T> result = new ExecuteResult<>();
//		result.code = returnCode.getCode();
//		result.success = FAIL;
//		result.msg = returnCode.getName();
//		return result;
//	}
}
